package com.google.gwt.gfx.client.graphics.vml;

import java.util.List;

import com.google.gwt.gfx.client.graphics.LinearRing;
import com.google.gwt.gfx.client.shared.Point;
import com.google.gwt.user.client.DOM;

public class VMLLinearRing extends LinearRing {

	public VMLLinearRing() {
		super(DOM.createElement("v:polyline"));
	}

	@Override
	protected void append(List<Point> points) {
		String originStr = points.get(0).getLeft() + " "
				+ points.get(0).getTop();
		String pointsStr = DOM.getElementProperty(getElement(), "points");

		StringBuilder builder = new StringBuilder();

		for (Point point : points) {
			builder.append(" " + point.getLeft() + " " + point.getTop());
		}

		DOM.setElementProperty(getElement(), "points",
				pointsStr.substring(0, pointsStr.length() - originStr.length())
						+ builder.toString() + " " + originStr);
	}

	@Override
	protected void append(Point point) {
		String originStr = point.getLeft() + " " + point.getTop();
		String pointsStr = DOM.getElementProperty(getElement(), "points");

		DOM.setElementProperty(getElement(), "points",
				pointsStr.substring(0, pointsStr.length() - originStr.length())
						+ " " + point.getLeft() + " " + point.getTop() + " "
						+ originStr);
	}

	@Override
	protected void update(List<Point> points) {
		StringBuilder builder = new StringBuilder();

		for (Point point : points) {
			builder.append(point.getLeft() + " " + point.getTop() + " ");
		}

		if (points.size() > 0) {
			Point origin = points.get(0);
			builder.append(origin.getLeft() + " " + origin.getTop());
		}

		DOM.setElementProperty(getElement(), "points", builder.toString());
	}

}
